﻿using System;
using LightContracts;
using System.Diagnostics;

namespace LightContractsExamples
{
	class Program
	{
		public static void Main(string[] args)
		{
			Console.WriteLine("Demonstration of LightContracts!");
			
			OnePreconditionFailed();
			
			AssertionWithoutStringConcatenation();
			AssertionWithStringConcatenation();
					
			Console.Write("Press any key to continue . . . ");
			Console.ReadKey(true);
		}
		
		public static void OnePreconditionFailed()
		{
			TextStorage storage = new TextStorage();
			try
			{
				// The statement will cause an AssertionException to be thrown
				storage.AddText(null);
			}
			catch(AssertException exception)
			{
				Console.WriteLine(exception.Message);
			}
		}
		
		public static void AssertionWithoutStringConcatenation()
		{
			const int max = 10000;
			Stopwatch watch = Stopwatch.StartNew();
			for(int i =0; i< max; i++)
			{
				Checks.That(i<max,"{0} is less than {1}",i,max);
			}
			watch.Stop();
			Console.WriteLine( max + " Assertions without string concatenation: " + watch.ElapsedTicks + " ticks");
		}
		
		public static void AssertionWithStringConcatenation()
		{
			const int max = 10000;
			Stopwatch watch = Stopwatch.StartNew();
			for(int i =0; i< max; i++)
			{
				Checks.That(i<max, i + " is less than " + max);
			}
			watch.Stop();
			Console.WriteLine( max + " Assertions with string concatenation   : " + watch.ElapsedTicks + " ticks" );
		}

	}
}